﻿//https://leetcode.cn/problems/gas-station/

class Solution {
public:
    int canCompleteCircuit(vector<int>& gas, vector<int>& cost)
    {
        int n = gas.size();
        for (int i = 0; i < n; i++)
        {
            //记录当前的汽油储值
            int rest = 0;
            int step = 0;  //从当前位置开始走的步数
            for (; step < n; step++)
            {
                int index = (i + step) % n;
                rest = rest + gas[index] - cost[index];
                if (rest < 0) break;
            }
            if (rest >= 0) return i;
            i = i + step;
        }

        return -1;

    }
};